package com.github.tetrisgame.ui.viewgroup;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.github.tetrisgame.utils.UIUtils;

public class search_layout extends ViewGroup {
    private SparseArray<Rect> childLayoutCache = new SparseArray<>();
    private boolean isShow = true;
    private int lines = 0;
    private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int textHeight = UIUtils.dp2px(10);

    public search_layout(Context context) {
        this(context,null);
    }

    public search_layout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        init();
    }
    private void init(){
        mPaint.setColor(Color.WHITE);

        mTextPaint.setTextSize(UIUtils.sp2px(10));
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int realWidth;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int realHeight;
        int desireWidth = 0;
        int desireHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            LayoutParams lp = child.getLayoutParams();
            int childWidthMeasureSpec = getChildMeasureSpec(
                    widthMeasureSpec,
                    getLeft()+getRight(),
                    lp.width
            );
            int childHeightMeasureSpec = getChildMeasureSpec(
                    heightMeasureSpec,
                    getTop()+getBottom(),
                    lp.height
            );
            child.measure(childWidthMeasureSpec,childHeightMeasureSpec);
            if(child.getMeasuredHeight() != 0){
                desireWidth += child.getMeasuredWidth();
            }else {
                desireWidth += child.getWidth();
            }
        }
        desireWidth += getPaddingLeft() + getPaddingRight();

        if(widthMode == MeasureSpec.EXACTLY){
            realWidth = widthSize;
        }else{
            if(widthSize >= desireWidth){
                realWidth = desireWidth;
            }else{
                realWidth = widthSize;
            }
        }
        if(isShow){
            int lineWidth = 0;
            lines = 0;
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                if(child.getWidth() != 0){
                    lineWidth += child.getWidth();
                }else {
                    lineWidth += child.getMeasuredWidth();
                }
                if(lineWidth > realWidth){
                    lineWidth = 0;
                    lines++;
                    i--;
                }
            }
            if(lineWidth > 0)lines++;
            if(getChildAt(0).getHeight() != 0){
                desireHeight = getChildAt(0).getHeight() * lines;
            }else {
                desireHeight = getChildAt(0).getMeasuredHeight() * lines;
            }

            desireHeight += getPaddingTop() + getPaddingBottom();
        }else{
            if(getChildAt(0).getHeight() != 0){
                desireHeight = getChildAt(0).getHeight();
            }else {
                desireHeight = getChildAt(0).getMeasuredHeight();
            }
        }
        desireHeight += textHeight;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if(heightMode == MeasureSpec.EXACTLY){
            realHeight = heightSize;
        }else{
            if(heightSize >= desireHeight){
                realHeight = desireHeight;
            }else{
                realHeight = heightSize;
            }
        }
        setMeasuredDimension(realWidth,realHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int paddintLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int childCount = getChildCount();
        int totalLength = getWidth()-getPaddingLeft() - getPaddingRight();
        int childWidth;
        int childHeight;
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            TextView a = (TextView) child;
            Log.v(a.getText().toString(),"qwe");
            if(child.getMeasuredHeight() != 0){
                childWidth = child.getMeasuredWidth();
                childHeight = child.getMeasuredHeight();
            }else {
                childWidth = child.getWidth();
                childHeight = child.getHeight();
            }
            if(totalLength - paddintLeft - childWidth < 0){
                paddingTop += childHeight;
                paddintLeft = getPaddingLeft();
            }
            Rect layoutRect = childLayoutCache.get(i);
            if (layoutRect == null) {
                // 首次计算位置并缓存
                if(child.getVisibility() != GONE){
                    int childLeft = paddintLeft;
                    int childTop = paddingTop;
                    child.layout(
                            childLeft,
                            childTop,
                            childLeft + childWidth,
                            childTop + childHeight

                    );
                    layoutRect = new Rect(
                            childLeft,
                            childTop,
                            childLeft + childWidth,
                            childTop + childHeight
                    );
                }
                childLayoutCache.put(i, layoutRect);
            }
            else{
                child.layout(
                        layoutRect.left,
                        layoutRect.top,
                        layoutRect.right,
                        layoutRect.bottom

                );
            }

            paddintLeft += childWidth;

        }
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            if(getHeight() - textHeight <= event.getY() && event.getY() <= getHeight()){
                isShow = !isShow;
                requestLayout();
                invalidate();
            }
        }
        return true;
    }

    @Override
    protected void dispatchDraw(@NonNull Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawRect(0,getHeight() - textHeight , getWidth(), getHeight(),mPaint);
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        canvas.drawText("收起",getWidth() / 2,getHeight() -fontMetrics.bottom,mTextPaint);
    }

}
